package edu.ccut.saturn.studio.dialog.dropsaturndata;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.w3c.dom.Document;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.logic.ShapesPlugin;
import edu.ccut.saturn.studio.logic.model.Shape;
import edu.ccut.saturn.studio.logic.model.drop.saturndata.SaturnDataPropertyLoader;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnDataNode;
import edu.ccut.saturn.studio.logic.model.xml.loader.DropComponetLoader;

public class SaturnsqlDataDialog extends TitleAreaDialog {
	
	private Shape saturnComponentNode;
	private Map<String, String> propertisMap = new HashMap<String, String>();
	private Combo combo;
	private Document databasedoc;

	public Shape getSaturnComponentNode() {
		return saturnComponentNode;
	}

	public void setSaturnComponentNode(Shape saturnComponentNode) {
		this.saturnComponentNode = saturnComponentNode;
	}

	public SaturnsqlDataDialog(Shell parentShell, SaturnDataNode saturnDataNode) {
		super(parentShell);
	}

	public SaturnsqlDataDialog(Shell parentShell, Document document) {
		super(parentShell);
		this.databasedoc = document;
	}

	protected Control createDialogArea(Composite parent) {
		Composite area = (Composite) super.createDialogArea(parent);
		Composite container = new Composite(area, SWT.NONE);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 3;
		container.setLayout(gridLayout);
		final GridData gd_container = new GridData(SWT.FILL, SWT.FILL, false,
				false);
		gd_container.heightHint = 81;
		gd_container.widthHint = 368;
		container.setLayoutData(gd_container);

		final Label label = new Label(container, SWT.NONE);
		label.setText(Messages.getString("SaturnDataDialog.type")); //$NON-NLS-1$

		final Button buttonCreat = new Button(container, SWT.RADIO);
		buttonCreat.setText(Messages.getString("SaturnDataDialog.create")); //$NON-NLS-1$
		buttonCreat.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				propertisMap = SaturnDataPropertyLoader
				.getSaturnDataPropertyMap(SaturnDataPropertyLoader.SATURNCREAT_PROPERTIES);
		combo.setItems(propertisMap.keySet().toArray(new String[] {}));
			}
		});

		final Button buttonSelect = new Button(container, SWT.RADIO);
		buttonSelect.setText(Messages.getString("SaturnDataDialog.select")); //$NON-NLS-1$
		buttonSelect.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				propertisMap = SaturnDataPropertyLoader
				.getSaturnDataPropertyMap(SaturnDataPropertyLoader.SATURN_SQL_QUERY_PROPERTIES);
		combo.setItems(propertisMap.keySet().toArray(new String[] {}));
			}
		});
		final Label label_1 = new Label(container, SWT.NONE);
		final GridData gd_label_1 = new GridData();
		gd_label_1.verticalIndent = 10;
		label_1.setLayoutData(gd_label_1);
		label_1.setText(Messages.getString("SaturnDataDialog.component")); //$NON-NLS-1$

		combo = new Combo(container, SWT.READ_ONLY);
		combo.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				if(!combo.getText().trim().equals("")){
					setErrorMessage(null);
				}
			}
		});
		final GridData gd_combo = new GridData(SWT.FILL, SWT.CENTER, true, false, 2,
				1);
		gd_combo.heightHint = 38;
		gd_combo.verticalIndent = 10;
		combo.setLayoutData(gd_combo);
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);
		setMessage(Messages.getString("SaturnDataDialog.CreateComponent")); //$NON-NLS-1$

		return area;
	}

	protected Point getInitialSize() {
		return new Point(400, 208);
	}

	protected int getShellStyle() {
		return SWT.CLOSE | SWT.RESIZE;
	}

	protected void buttonPressed(int buttonId) {
		if (buttonId == IDialogConstants.OK_ID) {
			if(combo.getText().trim().equals("")){
				setErrorMessage(Messages.getString("SaturnDataDialog.ChooseOneComponent")); //$NON-NLS-1$
				return;
			}
			
			Document doc = ShapesPlugin.getDefault().getMap().get(
					this.propertisMap.get(combo.getText().trim()));
			DropComponetLoader componetLoader = new DropComponetLoader();
			this.saturnComponentNode = componetLoader.getComponet(doc
					.getDocumentElement(),databasedoc);
			if (this.saturnComponentNode.getDialog().open() == 1) {
				saturnComponentNode = null;
			}
		}
		super.buttonPressed(buttonId);
	}

//	private Shape getSaturnComponentNodeByShapesList(List<Shape> shapeList) {
//		for (Shape shape : shapeList) {
//			return shape;
//		}
//		return null;
//	}

	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		super.createButtonsForButtonBar(parent);
		getButton(IDialogConstants.OK_ID).setEnabled(true);
	}
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setImage(Activator.getImage(Images.SATURN_ICONS_SMALLNAME));
	}
}
